home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / Visa.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  66.4 KB  |  1,332 lines

  1. #include-once
  2.  
  3. ; ------------------------------------------------------------------------------
  4. ;
  5. ; AutoIt Version: 3.0
  6. ; Language:       English
  7. ; Description:    VISA (GPIB & TCP) library for AutoIt
  8. ;                 Functions that allow controlling instruments (e.g. oscilloscopes,
  9. ;                 signal generators, spectrum analyzers, power supplies, etc)
  10. ;                 that have a GPIB or Ethernet port through the VISA interface
  11. ;                 (GPIB, TCP or Serial Interface)
  12. ; Limitations:    The VISA queries only return the 1st line of the device answer
  13. ;                 This is not a problem in most cases, as most devices will always
  14. ;                 answer with a single line.
  15. ; Notes:
  16. ;                 If you are interested in this library you probably already know
  17. ;                 what is VISA and GPIB, but here there is a short description
  18. ;                 for those that don't know about it:
  19. ;
  20. ;                 Basically GPIB allows you to control instruments like Power
  21. ;                 Supplies, Signal Generators, Oscilloscopes, Signal Generators, etc.
  22. ;                 You need to install or connect a GPIB interface card (PCI, PCMCIA
  23. ;                 or USB) to your PC and install the corresponding GPIB driver.
  24. ;
  25. ;                 VISA is a standard API that sits on top of the GPIB driver and
  26. ;                 it allows you to use the same programs to control your
  27. ;                 instruments regardless of the type of GPIB card that you have
  28. ;                 installed in your PC (most cards are made either by National
  29. ;                 Instruments(R) or by Agilent/Hewlett-Packard(R)).
  30. ;
  31. ;                 This library is that it opens AutoIt to a different kind of
  32. ;                 automation (instrument automation). Normally you would need to
  33. ;                 use some expensive "instrumentation" environment like
  34. ;                 Labwindows/CVI (TM), LabView (TM) or Matlab (TM) to automate
  35. ;                 instruments but now you can do so with AutoIt.
  36. ;                 The only requirement is that you need a VISA compatible GPIB
  37. ;                 card (all cards that I know are) and the corresponding VISA
  38. ;                 driver must be installed (look for visa32.dll in the
  39. ;                 windows\system32 folder).
  40. ;
  41. ;                 Basically you have 4 main functions:
  42. ;                 _viExecCommand - Executes commands and queries through GPIB
  43. ;                 _viOpen, _viClose - Open/Close a connection to a GPIB instrument.
  44. ;                 _viFindGpib - Find all the instruments in the GPIB bus
  45. ;
  46. ;                 There are other less important functions, like:
  47. ;                 _viGTL - Go to local mode (exeit the "remote control mode")
  48. ;                 _viGpibBusReset - Reset the GPIB bus if it is in a bad state
  49. ;                 _viSetTimeout - Sets the GPIB Query timeout
  50. ;                 _viSetAttribute - Set any VISA attribute
  51. ;
  52. ;                 There is one known limitation of this library:
  53. ;                 - The GPIB queries do not support binary transfer.
  54. ;
  55. ;                 It is recommended that you try first to execute the _viFindGpib
  56. ;                 function (as shown in the example in the _viFindGpib header)
  57. ;                 and see if you can find any instruments. You can also have a
  58. ;                 look at the examples in the _viExecCommand function description.
  59. ;
  60. ; ==============================================================================
  61. ; VERSION       DATE       DESCRIPTION
  62. ; -------    ----------    -----------------------------------------------------
  63. ; v1.0.00    02/01/2005    Initial release
  64. ; v1.0.01    02/06/2005    Formatted according to Standard UDF rules
  65. ;                          Fixed _viGpibBusReset
  66. ;                          Renamed _viFindGPIB to _viFindGpib
  67. ;                          Removed unnecessary MsgBox calls
  68. ;                          More detailed function headers
  69. ;                          Added Serial Interface related Attribute/Value Constants
  70. ; v1.0.02    02/11/2005    Fixed _viQueryf only executing "*IDN?" queries
  71. ;                          Fixed _viQueryf only returning characters up to the first space
  72. ;                          Fixed _viQuertf returning only first line of answer
  73. ;                          Added _viInterativeControl for interactive VISA control
  74. ;                          Added GPIB message termination attributes
  75. ; ------------------------------------------------------------------------------
  76.  
  77.  
  78. ; ==============================================================================
  79. ;- VISA Definitions ------------------------------------------------------------
  80. ; The VISA library requires some GLOBAL CONSTANTS and VARIABLES that are defined
  81. ; here:
  82.  
  83. ;- VISA CONSTANTS -------------------------------------------------------------
  84.  
  85. Global Const $VI_SUCCESS = 0 ; (0L)
  86. Global Const $VI_NULL = 0
  87.  
  88. Global Const $VI_TRUE = 1
  89. Global Const $VI_FALSE = 0
  90.  
  91. ;- VISA GPIB BUS control macros (for _viGpibControlREN, see below) -------------
  92. Global Const $VI_GPIB_REN_DEASSERT = 0
  93. Global Const $VI_GPIB_REN_ASSERT = 1
  94. Global Const $VI_GPIB_REN_DEASSERT_GTL = 2
  95. Global Const $VI_GPIB_REN_ASSERT_ADDRESS = 3
  96. Global Const $VI_GPIB_REN_ASSERT_LLO = 4
  97. Global Const $VI_GPIB_REN_ASSERT_ADDRESS_LLO = 5
  98. Global Const $VI_GPIB_REN_ADDRESS_GTL = 6
  99.  
  100.  
  101. ;- VISA interface ATTRIBUTE NAMES ----------------------------------------------
  102. ; General Attributes
  103. Global Const $VI_ATTR_TMO_VALUE = 0x3FFF001A
  104.  
  105. ; Serial Interface related Attributes
  106. Global Const $VI_ATTR_ASRL_BAUD = 0x3FFF0021
  107. Global Const $VI_ATTR_ASRL_DATA_BITS = 0x3FFF0022
  108. Global Const $VI_ATTR_ASRL_PARITY = 0x3FFF0023
  109. Global Const $VI_ATTR_ASRL_STOP_BITS = 0x3FFF0024
  110. Global Const $VI_ATTR_ASRL_FLOW_CNTRL = 0x3FFF0025
  111.  
  112. ; GPIB message termination attributes
  113. Global $VI_ATTR_TERMCHAR = 0x3FFF0018
  114. Global $VI_ATTR_TERMCHAR_EN = 0x3FFF0038
  115. Global $VI_ATTR_SEND_END_EN = 0x3FFF0016
  116.  
  117. ; NOTE: There are more attribute types. Please refer to the VISA Programmer's Guide
  118.  
  119.  
  120. ;- VISA interface ATTRIBUTE VALUES ---------------------------------------------
  121. ;* TIMEOUT VALUES:
  122. Global Const $VI_TMO_IMMEDIATE = 0
  123. Global Const $VI_TMO_INFINITE = 0xFFFFFFF
  124.  
  125. ; Serial Interface related Attribute Values
  126. Global Const $VI_ASRL_PAR_NONE = 0
  127. Global Const $VI_ASRL_PAR_ODD = 1
  128. Global Const $VI_ASRL_PAR_EVEN = 2
  129. Global Const $VI_ASRL_PAR_MARK = 3
  130. Global Const $VI_ASRL_PAR_SPACE = 4
  131.  
  132. Global Const $VI_ASRL_STOP_ONE = 10
  133. Global Const $VI_ASRL_STOP_ONE5 = 15
  134. Global Const $VI_ASRL_STOP_TWO = 20
  135.  
  136. Global Const $VI_ASRL_FLOW_NONE = 0
  137. Global Const $VI_ASRL_FLOW_XON_XOFF = 1
  138. Global Const $VI_ASRL_FLOW_RTS_CTS = 2
  139. Global Const $VI_ASRL_FLOW_DTR_DSR = 4
  140.  
  141. ; NOTE: There are more attribute values. Please refer to the VISA Programmer's Guide
  142.  
  143.  
  144. ;- VISA Global variable(s) -----------------------------------------------------
  145. ; The VISA Resource Manager is used by the _viOpen functions (see below)
  146. ; This is the only (non constant) Global required by this library
  147. Global $VISA_DEFAULT_RM = -1
  148.  
  149.  
  150.  
  151. ; ==============================================================================
  152. ;- Main VISA/GPIB functions ----------------------------------------------------
  153. ; These include _viExecCommand, _viOpen, _viClose and _viFindGpib
  154.  
  155. ;===============================================================================
  156. ;
  157. ; Description:      MAIN FUNCTION - Send a Command/Query to an Instrument/Device
  158. ; Syntax:           _viExecCommand($h_session, $s_command, $i_timeout_ms = -1)
  159. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session handle (INTEGER)
  160. ;                              * STRING -> A VISA DESCRIPTOR is a string which
  161. ;                                specifies the resource with which to establish a
  162. ;                                communication session. An example descriptor is
  163. ;                                "GPIB::20::0". This function supports all valid
  164. ;                                VISA descriptors, including GPIB, TCP, VXI and
  165. ;                                Serial Interface instruments. A detailed explanation
  166. ;                                of VISA descriptors is shown in the Notes section
  167. ;                                of this function.
  168. ;                                As a SHORTCUT you can use a STRING containing
  169. ;                                the address number (e.g. "20") of a GPIB
  170. ;                                instrument instead of typing the full descriptor
  171. ;                                (in that case, "GPIB::20::0")
  172. ;                              * INTEGER -> A VISA session handle is an integer
  173. ;                                value returned by _viOpen (see below).
  174. ;                                It is recommended that instead you use _viOpen
  175. ;                                and VISA session handles instead of descriptors
  176. ;                                if you plan to communicate repeteadly with an
  177. ;                                Instrument or Device, as otherwise each time that
  178. ;                                you contact the instrument you would incur the
  179. ;                                overhead of opening and closing the communication
  180. ;                                link.
  181. ;                                Once you are done using the instrument you must
  182. ;                                remember to close the link with _viClose (see below)
  183. ;                   $s_command - Command/Query to execute.
  184. ;                                A query MUST contain a QUESTION MARK (?)
  185. ;                                When the command is a QUERY the function will
  186. ;                                automatically wait for the instrument's answer
  187. ;                                (or until the operation times out)
  188. ;                   $i_timeout_ms - The operation timeout in MILISECONDS
  189. ;                                This is mostly important for QUERIES only
  190. ;                                This is an OPTIONAL PARAMETER.
  191. ;                                If it is not specified the last set timeout will
  192. ;                                be used. If it was never set before the default
  193. ;                                timeout (which depends on the VISA implementation)
  194. ;                                will be used. Timeouts can also be set separatelly
  195. ;                                with the _viSetTimeout function (see below)
  196. ;                   $s_mode - Control the mode in which the VISA viPrintf is called
  197. ;                                This is an OPTIONAL PARAMETER.
  198. ;                                Check the _viPrintf help for more info on
  199. ;                                this OPTIONAL PARAMETER (whose default value is @LF)
  200. ;                                This is normally NOT necessary and should only be set
  201. ;                                if your GPIB card or instrument require it.
  202. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  203. ;                   visa32.dll is in {WINDOWS}\system32)
  204. ;                   For GPIB communication a GPIB card (such as a National Instruments
  205. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  206. ; Return Value(s):  The return value depends on whether the command is a QUERY
  207. ;                   or not and in whether the operation was successful or not.
  208. ;
  209. ;                   * Command, NON QUERY:
  210. ;                     On Success - Returns ZERO
  211. ;                     On Failure - Returns -1 if the VISA DLL could not be open
  212. ;                                  or a NON ZERO value representing the VISA
  213. ;                                  error code (see the VISA programmer's guide)
  214. ;                   * QUERY:
  215. ;                     On Success - Returns the answer of the instrument to the QUERY
  216. ;                     On Failure - Returns -1 if the VISA DLL could not be open
  217. ;                                  Returns -3 if the VISA DLL returned an unexpected
  218. ;                                  number of results
  219. ;                                  or returns a NON ZERO value representing the VISA
  220. ;                                  error code (see the VISA programmer's guide)
  221. ;
  222. ;                   This function always sets @error to 1 in case of error
  223. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  224. ; Example(s):
  225. ;                 - Simple communication examples:
  226. ;                   Get instrument ID:
  227. ;                     $s_idn = _viExecCommand("GPIB::20::0","*IDN?")
  228. ;                   This is the same as:
  229. ;                     $s_idn = _viExecCommand("20","*IDN?") -> Note that "20" is a STRING
  230. ;
  231. ;                 - More efficient way to communicate many times
  232. ;                   You must use _viOpen and _viClose
  233. ;                   In this example we measure a POWER 100 times:
  234. ;                     $h_session = _viOpen("GPIB::1::0") ; or $h_session = _viOpen("1")
  235. ;                     For $n = 0 To 99
  236. ;                       $power_array[$n] = _viExecCommand($h_session,"POWER?")
  237. ;                     Next
  238. ;                     _viClose($h_session)
  239. ;
  240. ;                   A more complex example, using 2 instruments, a signal generator
  241. ;                   and a spectrum analyzer, to measure the average power error of
  242. ;                   the generator:
  243. ;
  244. ;                     $h_spec_analyzer = _viOpen("GPIB::1::0") ; or $h_session = _viOpen("1")
  245. ;                     $h_signal_gen = _viOpen("GPIB::12::0") ; or $h_session = _viOpen("1")
  246. ;                     $average_power_error = 0
  247. ;                     For $ideal_power = -100 To -10 ; dBM
  248. ;                       _viExecCommand($h_signal_gen,"SOURCE:POWER " & $ideal_power & "dBm")
  249. ;                       $current_power_error = Abs($ideal_power - _viExecCommand($h_spec_analyzer,"POWER?"))
  250. ;                       $average_power_error = $average_power_error + $current_power_error
  251. ;                     Next
  252. ;                     $average_power_error = $average_power_error / 91
  253. ;                     _viClose($h_spec_analyzer)
  254. ;                     _viClose($h_signal_gen)
  255. ;
  256. ; Note(s):
  257. ;                 The following is a description of the MOST COMMON VISA DESCRIPTORS
  258. ;                 Note that there are some more types. For more info please
  259. ;                 refer to a VISA programmer's guide (available at www.ni.com)
  260. ;                 Optional segments are shown in square brackets ([]).
  261. ;                 Required segments that must be filled in are denoted by angle
  262. ;                 brackets (<>).
  263. ;
  264. ;                 Interface   Syntax
  265. ;                 ------------------------------------------------------------
  266. ;                 GPIB INSTR      GPIB[board]::primary address
  267. ;                                 [::secondary address] [::INSTR]
  268. ;                 GPIB INTFC      GPIB[board]::INTFC
  269. ;                 TCPIP SOCKET    TCPIP[board]::host address::port::SOCKET
  270. ;                 Serial INSTR    ASRL[board][::INSTR]
  271. ;                 PXI INSTR       PXI[board]::device[::function][::INSTR]
  272. ;                 VXI INSTR       VXI[board]::VXI logical address[::INSTR]
  273. ;                 GPIB-VXI INSTR  GPIB-VXI[board]::VXI logical address[::INSTR]
  274. ;                 TCPIP INSTR     TCPIP[board]::host address[::LAN device name]
  275. ;                                 [::INSTR]
  276. ;
  277. ;                 The GPIB keyword is used for GPIB instruments.
  278. ;                 The TCPIP keyword is used for TCP/IP communication.
  279. ;                 The ASRL keyword is used for serial instruments.
  280. ;                 The PXI keyword is used for PXI instruments.
  281. ;                 The VXI keyword is used for VXI instruments via either embedded
  282. ;                 or MXIbus controllers.
  283. ;                 The GPIB-VXI keyword is used for VXI instruments via a GPIB-VXI
  284. ;                 controller.
  285. ;
  286. ;                 The default values for optional parameters are shown below.
  287. ;
  288. ;                 Optional Segment          Default Value
  289. ;                 ---------------------------------------
  290. ;                 board                     0
  291. ;                 secondary address         none
  292. ;                 LAN device name           inst0
  293. ;
  294. ;
  295. ;                 Example Resource Strings:
  296. ;                 --------------------------------------------------------------
  297. ;                 GPIB::1::0::INSTR     A GPIB device at primary address 1 and
  298. ;                                       secondary address 0 in GPIB interface 0.
  299. ;
  300. ;                 GPIB2::INTFC          Interface or raw resource for GPIB
  301. ;                                       interface 2.
  302. ;
  303. ;                 TCPIP0::1.2.3.4::999::SOCKET    Raw TCP/IP access to port 999
  304. ;                                                 at the specified IP address.
  305. ;
  306. ;                 ASRL1::INSTR          A serial device attached to interface
  307. ;                                       ASRL1.  VXI::MEMACC Board-level register
  308. ;                                       access to the VXI interface.
  309. ;
  310. ;                 PXI::15::INSTR        PXI device number 15 on bus 0.
  311. ;
  312. ;                 VXI0::1::INSTR        A VXI device at logical address 1 in VXI
  313. ;                                       interface VXI0.
  314. ;
  315. ;                 GPIB-VXI::9::INSTR    A VXI device at logical address 9 in a
  316. ;                                       GPIB-VXI controlled system.
  317. ;
  318. ;===============================================================================
  319.  
  320. Func _viExecCommand($h_session, $s_command, $i_timeout_ms = -1, $s_mode = @LF)
  321.     If StringInStr($s_command, "?") == 0 Then
  322.         ; The Command is NOT a QUERY
  323.         Return _viPrintf($h_session, $s_command, $i_timeout_ms, $s_mode)
  324.     Else
  325.         ; The Command is a QUERY
  326.         Return _viQueryf($h_session, $s_command, $i_timeout_ms)
  327.     EndIf
  328. EndFunc   ;==>_viExecCommand
  329.  
  330.  
  331. ;===============================================================================
  332. ;
  333. ; Description:      Opens a VISA connection to an Instrument/Device
  334. ; Syntax:           _viOpen($s_visa_address, $s_visa_secondary_address = 0)
  335. ; Parameter(s):     $s_visa_address - A VISA resource descriptor STRING (see the
  336. ;                   NOTES of _viExecCommand above for more info)
  337. ;                   As as shortcut you can also directly pass a GPIB address as
  338. ;                   an integer
  339. ;                   $s_visa_secondary_address - Some GPIB instruments have
  340. ;                   secondary addresses. This parameter is ZERO by default, which
  341. ;                   means NO SECONDARY ADDRESS.
  342. ;                   Only use this optional parameter if the primary address is
  343. ;                   passed as an integer
  344. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  345. ;                   visa32.dll is in {WINDOWS}\system32)
  346. ;                   For GPIB communication a GPIB card (such as a National Instruments
  347. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  348. ; Return Value(s):  On Success - Returns a (POSITIVE) VISA Instrument Handle
  349. ;                   On Failure - Returns -1 and SETS @error to 1
  350. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  351. ; Note(s):          For simple usage there is no need to use this function, as
  352. ;                   _viExecCommand automatically opens/closes a VISA connection
  353. ;                   if you pass it a VISA resource descriptor (see the NOTES of
  354. ;                   _viExecCommand above for more info)
  355. ;
  356. ;                   However, if you want to repeteadly send commands/queries to
  357. ;                   a device, you should call this function followed by using the
  358. ;                   returned instrument handle instead of the VISA descriptor
  359. ;
  360. ;                   Do not forget to use _viClose when you are done, though
  361. ;
  362. ;===============================================================================
  363.  
  364. Func _viOpen($s_visa_address, $s_visa_secondary_address = 0)
  365.     Local $h_session = -1 ; The session handle by default is invalid (-1)
  366.  
  367.     If IsNumber($s_visa_address) Or StringInStr($s_visa_address, "::") == 0 Then
  368.         ; We passed a number => Create the VISA string:
  369.         $s_visa_address = "GPIB0::" & $s_visa_address & "::" & $s_visa_secondary_address
  370.     EndIf
  371.  
  372.     ;- Do not open an instrument connection twice
  373.     ; TODO
  374.  
  375.     ;- Make sure that there is a Resource Manager open (Note: this will NOT open it twice!)
  376.     _viOpenDefaultRM()
  377.  
  378.     ;- Open the INSTRUMENT CONNECTION
  379.     ; errStatus = viOpen (VISA_DEFAULT_RM, "GPIB0::20::0", VI_NULL, VI_NULL, &h_session);
  380.     ; signed int viOpen(unsigned long, char*, unsigned long, unsigned long, *unsigned long)
  381.     Local $a_results
  382.     $a_results = DllCall("visa32.dll", "long", "viOpen", "long", $VISA_DEFAULT_RM, "str", $s_visa_address, "long", $VI_NULL, "long", $VI_NULL, "long_ptr", -1)
  383.     If @error <> 0 Then
  384.         ; Could not open VISA (visa32.dll)
  385.         ;MsgBox(16,"_viOpen - DllCall error","Could not open VISA (visa32.dll)")
  386.         Return -1
  387.     EndIf
  388.     Local $errStatus = $a_results[0]
  389.     If $errStatus <> 0 Then
  390.         ; Could not open VISA instrument/resource
  391.         SetError(1)
  392.         ;MsgBox(16,"VISA error","Could not open VISA instrument/resource: " & $s_visa_address)
  393.         Return -2
  394.     EndIf
  395.     ; Make sure that the DllCall returned enough values
  396.     If UBound($a_results) < 6 Then
  397.         SetError(1)
  398.         ;MsgBox(16,"VISA error","Call to viOpen did not return the right number of values")
  399.         Return -3
  400.     EndIf
  401.  
  402.     $h_session = $a_results[5]
  403.     If $h_session <= 0 Then
  404.         ; viOpen did not return a valid handle
  405.         SetError(1)
  406.         ;MsgBox(16,"VISA error","viOpen did not return a valid handle")
  407.         Return -4
  408.     EndIf
  409.  
  410.     ; We have a valid handle for the device
  411.     Return $h_session
  412. EndFunc   ;==>_viOpen
  413.  
  414.  
  415. ;===============================================================================
  416. ;
  417. ; Description:      Closes a VISA connection to an Instrument/Device
  418. ; Syntax:           _viClose($h_session)
  419. ; Parameter(s):     $h_session - A VISA session handle (as returned by _viOpen)
  420. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  421. ;                   visa32.dll is in {WINDOWS}\system32)
  422. ;                   For GPIB communication a GPIB card (such as a National Instruments
  423. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  424. ; Return Value(s):  On Success - Returns 0
  425. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  426. ;                                or a NON ZERO value representing the VISA
  427. ;                                error code (see the VISA programmer's guide)
  428.  
  429. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  430. ; Note(s):          For simple usage there is no need to use this function, as
  431. ;                   _viExecCommand automatically opens/closes a VISA connection
  432. ;                   if you pass it a VISA resource descriptor (see the NOTES of
  433. ;                   _viExecCommand above for more info)
  434. ;
  435. ;                   However, if you want to repeteadly send commands/queries to
  436. ;                   a device, you should use _viOpen followed by using the
  437. ;                   returned instrument handle instead of the VISA descriptor
  438. ;                   and then calling this function
  439. ;
  440. ;===============================================================================
  441.  
  442. Func _viClose($h_session)
  443.     ;- Close INSTRUMENT Connection
  444.     ; viClose(h_session);
  445.     Local $a_results
  446.     $a_results = DllCall("visa32.dll", "int", "viClose", "int", $h_session)
  447.     If @error <> 0 Then
  448.         ;MsgBox(16,"_viClose - DllCall error","Could not open VISA (visa32.dll)")
  449.         Return -1
  450.     EndIf
  451.     Local $errStatus = $a_results[0]
  452.     If $errStatus <> 0 Then
  453.         ; Could not close VISA instrument/resource
  454.         SetError(1)
  455.         ;MsgBox(16,"VISA error","Could not close VISA instrument/resource: " & $h_session)
  456.         Return $errStatus
  457.     EndIf
  458.  
  459.     Return 0
  460. EndFunc   ;==>_viClose
  461.  
  462.  
  463. ;===============================================================================
  464. ;
  465. ; Description:      Find all the DEVICES found in the GPIB bus
  466. ; Syntax:           _viFindGpib(ByRef $a_descriptor_list, ByRef $a_idn_list, $f_show_search_results = 0)
  467. ; Parameter(s):     $a_descriptor_list (ByRef) - RETURNS an array of the VISA resource
  468. ;                   descriptors (see the NOTES of _viExecCommand above for more
  469. ;                   info) of the instruments that were found in the GPIB bus
  470. ;                   $a_idn_list (ByRef) - RETURNS an array of the IDNs (i.e names)
  471. ;                   of the instruments that were found in the GPIB bus
  472. ;                   $f_show_search_results - If 1 a message box showing the
  473. ;                   results of the search will be shown
  474. ;                   The default is 0, which means that the results are not shown
  475. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  476. ;                   visa32.dll is in {WINDOWS}\system32)
  477. ;                   For GPIB communication a GPIB card (such as a National Instruments
  478. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  479. ; Return Value(s):  On Success - The number of instruments found (0 or more)
  480. ;                   On Failure - Returns a NEGATIVE value and SETS @error to 1
  481. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  482. ; Example(s):
  483. ;  ; This example performs a search on the GPIB bus and shows the results in a MsgBox
  484. ;  Dim $a_descriptor_list[1], $a_idn_list[1]
  485. ;  _viFindGpib($a_descriptor_list, $a_idn_list, 1)
  486. ;
  487. ; Note(s):          For simple usage there is no need to use this function, as
  488. ;                   _viExecCommand automatically opens/closes a VISA connection
  489. ;                   if you pass it a VISA resource descriptor (see the NOTES of
  490. ;                   _viExecCommand above for more info)
  491. ;
  492. ;                   However, if you want to repeteadly send commands/queries to
  493. ;                   a device, you should call this function followed by using the
  494. ;                   returned instrument handle instead of the VISA descriptor
  495. ;
  496. ;                   Do not forget to use _viClose when you are done, though
  497. ;
  498. ;===============================================================================
  499.  
  500. Func _viFindGpib(ByRef $a_descriptor_list, ByRef $a_idn_list, $f_show_search_results = 0)
  501.     ;- Make sure that there is a Resource Manager open (Note: this will NOT open it twice!)
  502.     _viOpenDefaultRM()
  503.  
  504.     ; Create the GPIB instrument list and return the 1st instrument descriptor
  505.     ; viStatus viFindRsrc (viSession, char*, *ViFindList, *ViUInt32, char*);
  506.     ; errStatus = viFindRsrc (VISA_DEFAULT_RM, "GPIB?*INSTR", &h_current_instr, &num_matches, s_found_instr_descriptor);
  507.     Local $a_results = DllCall("visa32.dll", "long", "viFindRsrc", _
  508.             "long", $VISA_DEFAULT_RM, "str", "GPIB?*INSTR", "long_ptr", -1, _
  509.             "int_ptr", -1, "str", "")
  510.     If @error <> 0 Then
  511.         ; Could not open VISA (visa32.dll)
  512.         ;MsgBox(16,"_viFindGpib - DllCall error","Could not open VISA (visa32.dll)")
  513.         Return -1
  514.     EndIf
  515.     Local $errStatus = $a_results[0]
  516.     If $errStatus <> 0 Then
  517.         ; Could not perform GPIB FIND operation
  518.         SetError(1)
  519.         ;MsgBox(16,"VISA error","Could not perform GPIB FIND operation")
  520.         Return -2
  521.     EndIf
  522.     ; Make sure that the DllCall returned enough values
  523.     If UBound($a_results) < 5 Then
  524.         SetError(1)
  525.         ;MsgBox(16,"VISA error","Call to viFindRsrc did not return the right number of values")
  526.         Return -3
  527.     EndIf
  528.  
  529.     ; Assign the outputs of the DllCall
  530.     Local $h_list_pointer = $a_results[3] ; The pointer to the list of found instruments
  531.     Local $i_num_instr = $a_results[4] ; The number of instruments that were found
  532.     Local $s_first_descriptor = $a_results[5] ; The descriptor of the first instrument found
  533.     If $i_num_instr < 1 Then ; No insturments were found
  534.         If $f_show_search_results == 1 Then
  535.             MsgBox(64, "GPIB search results", "NO INSTRUMENTS FOUND in the GPIB bus")
  536.         EndIf
  537.  
  538.         Return $i_num_instr
  539.     EndIf
  540.  
  541.     ; At least 1 instrument was found
  542.     ReDim $a_descriptor_list[$i_num_instr], $a_idn_list[$i_num_instr]
  543.     $a_descriptor_list[0] = $s_first_descriptor
  544.     ; Get the IDN of the 1st instrument
  545.     $a_idn_list[0] = _viExecCommand($s_first_descriptor, "*IDN?")
  546.  
  547.     ; Get the IDN of all the remaining instruments
  548.     For $n = 1 To $i_num_instr - 1
  549.         ; If more than 1 instrument was found, get the handle of the next instrument
  550.         ; and get its IDN
  551.  
  552.         ;- Get the handle and descriptor of the next instrument in the GPIB bus
  553.         ; We do this by calling "viFindNext"
  554.         ; viFindNext (*ViFindList, char*);
  555.         ; viFindNext (h_current_instr,s_found_instr_descriptor);
  556.         $a_results = DllCall("visa32.dll", "long", "viFindNext", "long", $h_list_pointer, "str", "")
  557.         If @error <> 0 Then
  558.             ; Could not open VISA (visa32.dll)
  559.             ;MsgBox(16,"_viFindGpib - DllCall error","Could not open VISA (visa32.dll)")
  560.             Return -1
  561.         EndIf
  562.         $errStatus = $a_results[0]
  563.         If $errStatus <> 0 Then
  564.             ; Could not perform GPIB FIND NEXT operation
  565.             SetError(1)
  566.             ;MsgBox(16,"VISA error","Could not perform GPIB FIND NEXT operation")
  567.             Return -2
  568.         EndIf
  569.         ; Make sure that the DllCall returned enough values
  570.         If UBound($a_results) < 3 Then
  571.             SetError(1)
  572.             ;MsgBox(16,"VISA error","Call to viFindNext did not return the right number of values")
  573.             Return -3
  574.         EndIf
  575.         $a_descriptor_list[$n] = $a_results[2]
  576.         $a_idn_list[$n] = _viExecCommand($a_descriptor_list[$n], "*IDN?")
  577.     Next
  578.  
  579.     If $f_show_search_results == 1 Then
  580.         ; Create the GPIB instrument list and show it in a MsgBox
  581.         Local $s_search_results = ""
  582.         For $n = 0 To $i_num_instr - 1
  583.             $s_search_results = $s_search_results & $a_descriptor_list[$n] & " - " & $a_idn_list[$n] & @CR
  584.         Next
  585.         MsgBox(64, "GPIB search results", $s_search_results)
  586.     EndIf
  587.  
  588.     Return $i_num_instr
  589.  
  590. EndFunc   ;==>_viFindGpib
  591.  
  592.  
  593. ;===============================================================================
  594. ;- Internal VISA functions, used by _viExecCommand, _viOpen and/or _viClose ----
  595. ; The functions in this section are not meant to be called outside this library
  596. ; under normal use
  597.  
  598. ;===============================================================================
  599. ;
  600. ; Description:      Open the VISA Resource Manager
  601. ; Syntax:           _viOpenDefaultRM()
  602. ; Parameter(s):     None
  603. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  604. ;                   visa32.dll is in {WINDOWS}\system32)
  605. ;                   For GPIB communication a GPIB card (such as a National Instruments
  606. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  607. ; Return Value(s):  On Success - The Default Resource Manager Handle (also stored
  608. ;                   in the $VISA_DEFAULT_RM global)
  609. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  610. ;                                Returns -2 if there was an error opening the
  611. ;                                Default Resource Manager
  612. ;                                Returns -3 if the returned Resource Manager is
  613. ;                                invalid
  614. ;                   This function always sets @error to 1 in case of error
  615. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  616. ; Note(s):          You should not need to directly call this function under
  617. ;                   normal use as _viOpen calls it when necessary
  618. ;
  619. ;===============================================================================
  620. Func _viOpenDefaultRM()
  621.     Local $h_visa_rm = $VISA_DEFAULT_RM
  622.     If $VISA_DEFAULT_RM < 0 Then
  623.         ; Only open the Resource Manager once (i.e. when $VISA_DEFAULT_RM is still -1)
  624.         $h_visa_rm = $VISA_DEFAULT_RM ; Initialize the output result with the default value (-1)
  625.  
  626.         ; errStatus = viOpenDefaultRM (&VISA_DEFAULT_RM);
  627.         ; signed int viOpenDefaultRM(*unsigned long)
  628.         Local $a_results
  629.         $a_results = DllCall("visa32.dll", "int", "viOpenDefaultRM", "int_ptr", $VISA_DEFAULT_RM)
  630.         If @error <> 0 Then
  631.             ; Could not open VISA (visa32.dll)
  632.             ;MsgBox(16,"_viOpenDefaultRM - DllCall error","Could not open VISA (visa32.dll)")
  633.             Return -1
  634.         EndIf
  635.         Local $errStatus = $a_results[0]
  636.         If $errStatus <> 0 Then
  637.             ; Could not create VISA Resource Manager
  638.             SetError(1)
  639.             ;MsgBox(16,"VISA error","Could not create VISA Resource Manager")
  640.             Return -2
  641.         EndIf
  642.         ; Everything went fine => Set the Resource Manager global
  643.         $VISA_DEFAULT_RM = $a_results[1]
  644.         If $VISA_DEFAULT_RM <= 0 Then
  645.             ; There was an error, reset the $VISA_DEFAULT_RM
  646.             $VISA_DEFAULT_RM = -1 ; Default value
  647.             SetError(1)
  648.             Return -3
  649.         EndIf
  650.         $h_visa_rm = $VISA_DEFAULT_RM
  651.     EndIf
  652.  
  653.     Return $h_visa_rm
  654. EndFunc   ;==>_viOpenDefaultRM
  655.  
  656.  
  657. ;===============================================================================
  658. ;
  659. ; Description:      Send a COMMAND (NOT a QUERY) to an Instrument/Device
  660. ; Syntax:           _viPrintf($h_session, $s_command, $i_timeout_ms = -1)
  661. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session handle (INTEGER)
  662. ;                                Look at the _viExecCommand function for more
  663. ;                                details
  664. ;                   $s_command - Command/Query to execute.
  665. ;                                A query MUST contain a QUESTION MARK (?)
  666. ;                                When the command is a QUERY the function will
  667. ;                                automatically wait for the instrument's answer
  668. ;                                (or until the operation times out)
  669. ;                   $i_timeout_ms - The operation timeout in MILISECONDS
  670. ;                                This is mostly important for QUERIES only
  671. ;                                This is an OPTIONAL PARAMETER.
  672. ;                                If it is not specified the last set timeout will
  673. ;                                be used. If it was never set before the default
  674. ;                                timeout (which depends on the VISA implementation)
  675. ;                                will be used. Timeouts can also be set separatelly
  676. ;                                with the _viSetTimeout function (see below).
  677. ;                                Depending on the bus type (GPIB, TCP, etc) the
  678. ;                                timeout might not be set to the exact value that
  679. ;                                you request. Instead the closest valid timeout
  680. ;                                bigger than the one that you requested will be used.
  681. ;                   $s_mode - Control the mode in which the VISA viPrintf is called
  682. ;                                This is an OPTIONAL PARAMETER
  683. ;                                The DEFAULT VALUE is @LF, which means "attach @LF mode".
  684. ;                                Some instruments and in particular many GPIB cards
  685. ;                                Do not honor the terminator character attribute
  686. ;                                In those cases an @LF terminator needs to be added.
  687. ;                                As this is the most common case, by default the mode
  688. ;                                is set to @LF, which appends @LF to the SCPI command
  689. ;                                You can also set this mode to @CR and @CRLF if your card
  690. ;                                uses those terminators.
  691. ;                                If you do not want to use a terminator, set this parameter
  692. ;                                to an empty string ("")
  693. ;                                Also, some cards support the execution of a "sprintf" on the
  694. ;                                SCPI string prior to sending it through the VISA interface.
  695. ;                                For those who do, it is possible, by setting this
  696. ;                                parameter to "str" to "protect" the VISa interface from
  697. ;                                accidentally applying an escape sequence when a "/" is
  698. ;                                found within the VISA command string.
  699. ;                                This is normally NOT necessary and should only be set
  700. ;                                if your GPIB card or instrument require it.
  701. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  702. ;                   visa32.dll is in {WINDOWS}\system32)
  703. ;                   For GPIB communication a GPIB card (such as a National Instruments
  704. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  705. ; Return Value(s):  On Success - Returns ZERO
  706. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  707. ;                                or a NON ZERO value representing the VISA
  708. ;                                error code (see the VISA programmer's guide)
  709. ;                   This function always sets @error to 1 in case of error
  710. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  711. ; Note(s):
  712. ;                   Normally you do not need to use this function,
  713. ;                   as _viExecCommand automatically choses between _viPrintf and
  714. ;                   _viQueryf depending on the command type.
  715. ;
  716. ;                   If you need to use it anyway, it is recommended that you do
  717. ;                   not use this command for sending QUERIES, only for GPIB
  718. ;                   commands that DO NOT RETURN AN ANSWER
  719. ;
  720. ;                   Also, this is not really a "PRINTF-like" function, as it
  721. ;                   does not allow you to pass multiple parameters. This is only
  722. ;                   called _viPrintf because it uses the VISA function viPrintf
  723. ;
  724. ;                   See _viExecCommand for more details
  725. ;
  726. ;===============================================================================
  727.  
  728. Func _viPrintf($h_session, $s_command, $i_timeout_ms = -1, $s_option = @LF)
  729.     Local $f_close_session_before_return = 0 ; By default do not close the session at the end
  730.     If IsString($h_session) Then
  731.         ; When we pass a string, i.e. a VISA ID (like GPIB::20::0, for instance) instead
  732.         ; of a VISA session handler, we will automatically OPEN and CLOSE the instrument
  733.         ; session for the user.
  734.         ; This is of course slower if you need to do more than one GPIB call but much
  735.         ; more convenient for short tests
  736.         $f_close_session_before_return = 1
  737.         $h_session = _viOpen($h_session)
  738.     EndIf
  739.  
  740.     ;- Set the VISA timeout if necessary
  741.     If $i_timeout_ms >= 0 Then
  742.         _viSetTimeout($h_session, $i_timeout_ms)
  743.     EndIf
  744.  
  745.     ;- Send Command to instrument (using viPrintf VISA function)
  746.     ; The syntax of the viPrintf VISA function is:
  747.     ; errStatus = viPrintf (h_session, "%s", "*RST");
  748.     ; signed int viPrintf (unsigned long, char*, char*);
  749.  
  750.     ; For symmetry with the viQueryf function, and to solve compatibility issues
  751.     ; with some instruments, call viPrintf WITHOUT protecting from escape sequences
  752.     ; The user MUST thus be careful when passing commands containing the '/' character
  753.     Local $a_results
  754.     Select
  755.         Case $s_option == "str"
  756.             ; Use the "str" mode to pass the SCPI command to the VISA interface
  757.             $a_results = DllCall("visa32.dll", "int", "viPrintf", "int", $h_session, "str", "%s", "str", $s_command) ; Call viPrintf with escape sequence protection
  758.         Case ($s_option == @CR Or $s_option == @LF Or $s_option == @CRLF)
  759.             ; Append the selected terminator to the SCPI command
  760.             $a_results = DllCall("visa32.dll", "int", "viPrintf", "int", $h_session, "str", $s_command & $s_option)
  761.         Case Else ; In all other cases, ignore the "mode" and do not use any terminator string
  762.             $a_results = DllCall("visa32.dll", "int", "viPrintf", "int", $h_session, "str", $s_command) ; Call viPrintf without escape sequence protection
  763.     EndSelect
  764.  
  765.     If @error <> 0 Then
  766.         ; Could not open VISA (visa32.dll)
  767.         ;MsgBox(16,"_viPrintf - DllCall error","Could not open VISA (visa32.dll)")
  768.         Return -1
  769.     EndIf
  770.     Local $errStatus = $a_results[0]
  771.     If $errStatus <> 0 Then
  772.         ; Could not send command to VISA instrument/resource
  773.         SetError(1)
  774.         ;MsgBox(16,"VISA error","Could not send command to VISA instrument/resource: " & $h_session)
  775.         Return $errStatus
  776.     EndIf
  777.  
  778.     If $f_close_session_before_return == 1 Then
  779.         _viClose($h_session)
  780.     EndIf
  781. EndFunc   ;==>_viPrintf
  782.  
  783.  
  784. ;===============================================================================
  785. ;
  786. ; Description:      Send a QUERY (a Command that returns an answer) to an Instrument/Device
  787. ; Syntax:           _viQueryf($h_session, $s_query, $i_timeout_ms = -1)
  788. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session handle (INTEGER)
  789. ;                                Look at the _viExecCommand function for more
  790. ;                                details
  791. ;                   $s_command - The query to execute (e.g. "*IDN?").
  792. ;                                A query MUST contain a QUESTION MARK (?)
  793. ;                                The function willautomatically wait for the
  794. ;                                instrument's answer (or until the operation
  795. ;                                times out)
  796. ;                   $i_timeout_ms - The operation timeout in MILISECONDS
  797. ;                                This is mostly important for QUERIES only
  798. ;                                This is an OPTIONAL PARAMETER.
  799. ;                                If it is not specified the last set timeout will
  800. ;                                be used. If it was never set before the default
  801. ;                                timeout (which depends on the VISA implementation)
  802. ;                                will be used. Timeouts can also be set separatelly
  803. ;                                with the _viSetTimeout function (see below)
  804. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  805. ;                   visa32.dll is in {WINDOWS}\system32)
  806. ;                   For GPIB communication a GPIB card (such as a National Instruments
  807. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  808. ; Return Value(s):  On Success - Returns a STRING containing the answer of the
  809. ;                                instrument to the QUERY
  810. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  811. ;                                Returns -3 if the VISA DLL returned an unexpected
  812. ;                                number of results
  813. ;                                or returns a NON ZERO value representing the VISA
  814. ;                                error code (see the VISA programmer's guide)
  815. ;                   This function always sets @error to 1 in case of error
  816. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  817. ; Note(s):
  818. ;                   Normally you do not need to use this function,
  819. ;                   as _viExecCommand automatically choses between _viPrintf and
  820. ;                   _viQueryf depending on the command type.
  821. ;
  822. ;                   If you need to use it anyway, make sure that you use it for
  823. ;                   a command that RETURNS an ANSWER or you will be stuck until
  824. ;                   the Timeout expires, which could never happen if the Timeout
  825. ;                   is infinite ("INF")!
  826. ;
  827. ;                   Also, this is not really a "SCANF-like" function, as it
  828. ;                   does not allow you to specify the format of the output
  829. ;
  830. ;                   There are two known limitations of this function:
  831. ;                   - The GPIB queries only return the 1st line of the device
  832. ;                     answer. This is normally not a problem as most devices
  833. ;                     always return a single line answer.
  834. ;                   - The GPIB queries do not support binary transfer.
  835. ;
  836. ;                   See _viExecCommand for more details
  837. ;
  838. ;===============================================================================
  839. Func _viQueryf($h_session, $s_query, $i_timeout_ms = -1)
  840.     Local $f_close_session_before_return = 0 ; By default do not close the session at the end
  841.     If IsString($h_session) Then
  842.         ; When we pass a string, i.e. a VISA ID (like GPIB::20::0, for instance) instead
  843.         ; of a VISA session handler, we will automatically OPEN and CLOSE the instrument
  844.         ; session for the user.
  845.         ; This is of course slower if you need to do more than one GPIB call but much
  846.         ; more convenient for short tests
  847.         $f_close_session_before_return = 1
  848.         $h_session = _viOpen($h_session)
  849.     EndIf
  850.  
  851.     ;- Set the VISA timeout if necessary
  852.     If $i_timeout_ms >= 0 Then
  853.         _viSetTimeout($h_session, $i_timeout_ms)
  854.     EndIf
  855.  
  856.     ;- Send QUERY to instrument and get ANSWER
  857.     ; errStatus = viQueryf (h_session, "*IDN?\n", "%s", s_answer);
  858.     ; signed int viQueryf (unsigned long, char*, char*, char*);
  859.     ;errStatus = viQueryf (h_instr, s_command, "%s", string);
  860.     Local $a_results, $s_answer = ""
  861.     $a_results = DllCall("visa32.dll", "int", "viQueryf", "int", $h_session, "str", $s_query, "str", "%t", "str", $s_answer)
  862.     If @error <> 0 Then
  863.         ; Could not open VISA (visa32.dll)
  864.         ;MsgBox(16,"_viQueryf - DllCall error","Could not open VISA (visa32.dll)")
  865.         Return -1
  866.     EndIf
  867.     Local $errStatus = $a_results[0]
  868.     If $errStatus <> 0 Then
  869.         ; Could not query VISA instrument/resource
  870.         SetError(1)
  871.         ;MsgBox(16,"VISA error","Could not query VISA instrument/resource: " & $h_session)
  872.         Return $errStatus
  873.     EndIf
  874.     ; Make sure that the DllCall returned enough values
  875.     If UBound($a_results) < 5 Then
  876.         ; Call to viQuery did not return the right number of values
  877.         SetError(1)
  878.         ;MsgBox(16,"VISA error","Call to viQuery did not return the right number of values")
  879.         Return -3
  880.     EndIf
  881.     $s_answer = $a_results[4]
  882.  
  883.     If $f_close_session_before_return == 1 Then
  884.         _viClose($h_session)
  885.     EndIf
  886.  
  887.     Return $s_answer
  888. EndFunc   ;==>_viQueryf
  889.  
  890.  
  891. ;- Misc VISA interface functions -----------------------------------------------
  892.  
  893. ;===============================================================================
  894. ;
  895. ; Description:      Sets the VISA timeout in MILISECONDS (uses _viSetAttribute)
  896. ; Syntax:           _viSetTimeout($h_session, $i_timeout_ms)
  897. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session
  898. ;                   handle (INTEGER). Look the explanation in _viExecCommand
  899. ;                   (you can find it above)
  900. ;                   $i_timeout_ms - The timeout IN MILISECONDS for VISA operations
  901. ;                   (mainly for GPIB queries)
  902. ;                   If you set it to 0 the tiemouts are DISABLED
  903. ;                   If you set it to "INF" the VISA operations will NEVER timeout.
  904. ;                   Be careful with this as it could easly hung your program if
  905. ;                   your instrument does not respond to one of your queries
  906. ;                   Depending on the bus type (GPIB, TCP, etc) the timeout might
  907. ;                   not be set to the exact value that you request. Instead the
  908. ;                   closest valid timeout bigger than the one that you requested
  909. ;                   will be used.
  910. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  911. ;                   visa32.dll is in {WINDOWS}\system32)
  912. ;                   For GPIB communication a GPIB card (such as a National Instruments
  913. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  914. ; Return Value(s):  On Success - Returns 0
  915. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  916. ;                                or a NON ZERO value representing the VISA
  917. ;                                error code (see the VISA programmer's guide)
  918. ;                   This function always sets @error to 1 in case of error
  919. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  920. ; Note(s):          You can avoid directly calling this function most of the time,
  921. ;                   as _viExecCommand accepts a timeout (in ms) as its 3rd argument.
  922. ;                   If you do not pass this 3rd argument then the previous timeout
  923. ;                   will be used (or the default timeout, which depends on the
  924. ;                   VISA driver, if it was never set before)
  925. ;
  926. ;===============================================================================
  927. Func _viSetTimeout($h_session, $i_timeout_ms)
  928.     If StringUpper(String($i_timeout_ms)) == "INF" Then
  929.         $i_timeout_ms = $VI_TMO_INFINITE
  930.     EndIf
  931.     Return _viSetAttribute($h_session, $VI_ATTR_TMO_VALUE, $i_timeout_ms)
  932. EndFunc   ;==>_viSetTimeout
  933.  
  934.  
  935. ;===============================================================================
  936. ;
  937. ; Description:      VISA attribute set (GENERIC)
  938. ;                   Called by _viSetTimeout, this function can ALSO be used to
  939. ;                   set many other VISA specific attributes, like the Serial
  940. ;                   Interface Attributes.
  941. ;                   Read the VISA documentation for more information
  942. ; Syntax:           _viSetAttribute($h_session, $i_attribute, $i_value)
  943. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session
  944. ;                   handle (INTEGER). Look the explanation in _viExecCommand
  945. ;                   (you can find it above)
  946. ;                   $i_attribute - The index of the attribute that must be changed
  947. ;                   Attributes are defined in the VISA library. This AutoIt
  948. ;                   implementation only defines a CONSTANT for the TIMEOUT
  949. ;                   attribute ($VI_ATTR_TMO_VALUE) but you can pass any other
  950. ;                   index if you want to.
  951. ;                   $i_value - The value of the attribute. It must be an integer
  952. ;                   and the possible values depend on the attribute type
  953. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  954. ;                   visa32.dll is in {WINDOWS}\system32)
  955. ;                   For GPIB communication a GPIB card (such as a National Instruments
  956. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  957. ; Return Value(s):  On Success - Returns 0
  958. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  959. ;                                or a NON ZERO value representing the VISA
  960. ;                                error code (see the VISA programmer's guide)
  961. ;                   This function always sets @error to 1 in case of error
  962. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  963. ; Note(s):
  964. ;                   This is a list of the currently pre-defined attributes and
  965. ;                   values. Remember that you can use any other valid
  966. ;                   attribute/value by passing the corresponding integer index
  967. ;                   (as defined in the VISA programmer's guide) to this function.
  968. ;
  969. ;                   * Attribute: $VI_ATTR_TMO_VALUE -> Set Timeout
  970. ;                   * Values:
  971. ;                             A timeout in MILLISECONDS or
  972. ;                             $VI_TMO_IMMEDIATE (or 0) for "Return immediatly"
  973. ;                             VI_TMO_INFINITE (or "INF") for "No timeout"
  974. ;
  975. ;                   * Attribute: $VI_ATTR_TERMCHAR -> Set Termination Character
  976. ;                   * Values:
  977. ;                             The ASCII code number of the terminator character
  978. ;                             which is used for VISA messages.
  979. ;                             - Typical values are:
  980. ;                                0x0A: Linefeed or newline ("\n")
  981. ;                                0x0C: Form feed ("\f")
  982. ;                                0x0D: Carriage return ("\r")
  983. ;
  984. ;                   * Attribute: $VI_ATTR_TERMCHAR_EN -> Set Termination Character
  985. ;                   * Values:
  986. ;                             For many instruments this attribute has no effect.
  987. ;                             For those who take it into account:
  988. ;                             $VI_FALSE: Wait for the TIMEOUT before returning from
  989. ;                                        a viRead operation
  990. ;                             $VI_TRUE: Allow read operations to terminate as soon
  991. ;                                       as the "VI_ATTR_TERMCHAR" character is received
  992. ;                                       during a viRead.
  993. ;                             which is used for VISA messages (e.g. 10)
  994. ;                             $VI_TMO_IMMEDIATE (or 0) for "Return immediatly"
  995. ;                             VI_TMO_INFINITE (or "INF") for "No timeout"
  996. ;                   * Default Value: $VI_FALSE. Note that many instruments ignore this.
  997. ;
  998. ;                   * Attribute: $VI_ATTR_ASRL_BAUD
  999. ;                   * Values:
  1000. ;                             Any valid baudrate (9600, 115200, etc)
  1001. ;
  1002. ;                   * Attribute: $VI_ATTR_ASRL_DATA_BITS
  1003. ;                   * Values:
  1004. ;                             From 5 to 8
  1005. ;
  1006. ;                   * Attribute: $VI_ATTR_ASRL_PARITY
  1007. ;                   * Values:
  1008. ;                             $VI_ASRL_PAR_NONE
  1009. ;                             $VI_ASRL_PAR_ODD
  1010. ;                             $VI_ASRL_PAR_EVEN
  1011. ;                             $VI_ASRL_PAR_MARK
  1012. ;                             $VI_ASRL_PAR_SPACE
  1013. ;
  1014. ;                   * Attribute: $VI_ATTR_ASRL_STOP_BITS
  1015. ;                   * Values:
  1016. ;                             $VI_ASRL_STOP_ONE
  1017. ;                             $VI_ASRL_STOP_ONE5
  1018. ;                             $VI_ASRL_STOP_TWO
  1019. ;
  1020. ;                   * Attribute: $VI_ATTR_ASRL_FLOW_CNTRL
  1021. ;                   * Values:
  1022. ;                             $VI_ASRL_FLOW_NONE
  1023. ;                             $VI_ASRL_FLOW_XON_XOFF
  1024. ;                             $VI_ASRL_FLOW_RTS_CTS
  1025. ;                             $VI_ASRL_FLOW_DTR_DSR
  1026.  
  1027.  
  1028. ;
  1029. ;===============================================================================
  1030. Func _viSetAttribute($h_session, $i_attribute, $i_value)
  1031.     Local $f_close_session_before_return = 0 ; By default do not close the session at the end
  1032.     If IsString($h_session) Then
  1033.         ; When we pass a string, i.e. a VISA ID (like GPIB::20::0, for instance) instead
  1034.         ; of a VISA session handler, we will automatically OPEN and CLOSE the instrument
  1035.         ; session for the user.
  1036.         ; This is of course slower if you need to do more than one GPIB call but much
  1037.         ; more convenient for short tests
  1038.         $f_close_session_before_return = 1
  1039.         $h_session = _viOpen($h_session)
  1040.     EndIf
  1041.  
  1042.     ; errStatus = _viSetAttribute ($h_session, $VI_ATTR_TMO_VALUE, $timeout_value);
  1043.     ; signed int viGpibControlREN (unsigned long, int, int);
  1044.     Local $a_results
  1045.     $a_results = DllCall("visa32.dll", "int", "viSetAttribute", "int", $h_session, "int", $i_attribute, "int", $i_value)
  1046.     If @error <> 0 Then
  1047.         ; Could not open VISA (visa32.dll)
  1048.         ;MsgBox(16,"_viSetAttribute - DllCall error","Could not open VISA (visa32.dll)")
  1049.         Return -1
  1050.     EndIf
  1051.     Local $errStatus = $a_results[0]
  1052.     If $errStatus <> 0 Then
  1053.         ; Could not set attribute of VISA instrument/resource
  1054.         SetError(1)
  1055.         ;MsgBox(16,"VISA error","Could not set attribute of VISA instrument/resource: " & $h_session)
  1056.         Return $errStatus
  1057.     EndIf
  1058.  
  1059.     If $f_close_session_before_return == 1 Then
  1060.         _viClose($h_session)
  1061.     EndIf
  1062.  
  1063.     Return 0
  1064. EndFunc   ;==>_viSetAttribute
  1065.  
  1066.  
  1067. ;===============================================================================
  1068. ;
  1069. ; Description:      Go To Local mode (uses _viGpibControlREN)
  1070. ;                   Instruments that accept this command will exit the "Remote
  1071. ;                   Control mode" and go to "Local mode"
  1072. ;                   If the instrument is already in "Local mode" this is simply
  1073. ;                   ignored.
  1074. ;                   Normally, if an instrument does not support this command it
  1075. ;                   will simply stay in the "Remote Control mode"
  1076. ; Syntax:           _viGTL($h_session)
  1077. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session
  1078. ;                   handle (INTEGER). Look the explanation in _viExecCommand
  1079. ;                   (you can find it above)
  1080. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  1081. ;                   visa32.dll is in {WINDOWS}\system32)
  1082. ;                   For GPIB communication a GPIB card (such as a National Instruments
  1083. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  1084. ;                   This function always sets @error to 1 in case of error
  1085. ; Return Value(s):  On Success - Returns 0
  1086. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  1087. ;                                or a NON ZERO value representing the VISA
  1088. ;                                error code (see the VISA programmer's guide)
  1089. ;                   This function always sets @error to 1 in case of error
  1090. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  1091. ; Note(s):          None
  1092. ;
  1093. ;===============================================================================
  1094. Func _viGTL($h_session)
  1095.     Return _viGpibControlREN($h_session, $VI_GPIB_REN_ADDRESS_GTL)
  1096. EndFunc   ;==>_viGTL
  1097.  
  1098.  
  1099. ;===============================================================================
  1100. ;
  1101. ; Description:      GPIB BUS "reset" (uses _viGpibControlREN)
  1102. ;                   Use this function when the GPIB BUS gets stuck for some reason.
  1103. ;                   You might be lucky and resolve the problem by calling this
  1104. ;                   function
  1105. ; Syntax:           _viGpibBusReset()
  1106. ; Parameter(s):     None
  1107. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  1108. ;                   visa32.dll is in {WINDOWS}\system32)
  1109. ;                   For GPIB communication a GPIB card (such as a National Instruments
  1110. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  1111. ; Return Value(s):  On Success - Returns 0
  1112. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  1113. ;                                or a NON ZERO value representing the VISA
  1114. ;                                error code (see the VISA programmer's guide)
  1115. ;                   This function always sets @error to 1 in case of error
  1116. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  1117. ; Note(s):          None
  1118. ;
  1119. ;===============================================================================
  1120. Func _viGpibBusReset()
  1121.     Return _viGpibControlREN("GPIB0::INTFC", $VI_GPIB_REN_DEASSERT)
  1122. EndFunc   ;==>_viGpibBusReset
  1123.  
  1124.  
  1125. ;===============================================================================
  1126. ;
  1127. ; Description:      Control the VISA REN bus line
  1128. ; Syntax:           _viGpibControlREN ($h_session, $i_mode)
  1129. ; Parameter(s):     $h_session - A VISA descriptor (STRING) OR a VISA session
  1130. ;                   handle (INTEGER). Look the explanation in _viExecCommand
  1131. ;                   (you can find it above)
  1132. ;                   $i_mode - The mode into which the REN line of the GPIB bus
  1133. ;                   will be set.
  1134. ;                   Modes are defined in the VISA library. Look at the top of
  1135. ;                   this file for valid modes
  1136. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  1137. ;                   visa32.dll is in {WINDOWS}\system32)
  1138. ;                   For GPIB communication a GPIB card (such as a National Instruments
  1139. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  1140. ; Return Value(s):  On Success - Returns 0
  1141. ;                   On Failure - Returns -1 if the VISA DLL could not be open
  1142. ;                                or a NON ZERO value representing the VISA
  1143. ;                                error code (see the VISA programmer's guide)
  1144. ;                   This function always sets @error to 1 in case of error
  1145. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  1146. ; Note(s):          This function is used by _viGTL and _viGpibBusReset
  1147. ;
  1148. ;===============================================================================
  1149. Func _viGpibControlREN($h_session, $i_mode)
  1150.     Local $f_close_session_before_return = 0 ; By default do not close the session at the end
  1151.     If IsString($h_session) Then
  1152.         ; When we pass a string, i.e. a VISA ID (like GPIB::20::0, for instance) instead
  1153.         ; of a VISA session handler, we will automatically OPEN and CLOSE the instrument
  1154.         ; session for the user.
  1155.         ; This is of course slower if you need to do more than one GPIB call but much
  1156.         ; more convenient for short tests
  1157.         $f_close_session_before_return = 1
  1158.         $h_session = _viOpen($h_session)
  1159.     EndIf
  1160.  
  1161.     ; errStatus = viGpibControlREN ($h_session, VI_GPIB_REN_ASSERT);
  1162.     ; signed int viGpibControlREN (unsigned long, int);
  1163.     Local $a_results
  1164.     $a_results = DllCall("visa32.dll", "int", "viGpibControlREN", "int", $h_session, "int", $i_mode)
  1165.     If @error <> 0 Then
  1166.         ; Could not open VISA (visa32.dll)
  1167.         ;MsgBox(16,"_viGpibControlREN - DllCall error","Could not open VISA (visa32.dll)")
  1168.         Return -1
  1169.     EndIf
  1170.     Local $errStatus = $a_results[0]
  1171.     If $errStatus <> 0 Then
  1172.         ; Could not send to Local VISA instrument/resource
  1173.         SetError(1)
  1174.         ;MsgBox(16,"VISA error","Could not send to Local VISA instrument/resource: " & $h_session)
  1175.         Return $errStatus
  1176.     EndIf
  1177.  
  1178.     If $f_close_session_before_return == 1 Then
  1179.         _viClose($h_session)
  1180.     EndIf
  1181.  
  1182.     Return 0
  1183. EndFunc   ;==>_viGpibControlREN
  1184.  
  1185.  
  1186. ;===============================================================================
  1187. ;
  1188. ; Description:      Interactive VISA control.
  1189. ;                   This function lets you easily test your SCPI commands
  1190. ;                   interactively.
  1191. ;                   It also lets you save these commands into a file
  1192. ;                   Simply answer the questions (Device Descriptor, SCPI command
  1193. ;                   and timeout).
  1194. ;                   * If you click Cancel on the 1st question the interactive
  1195. ;                     control ends.
  1196. ;                   * If you click Cancel to the other queries, you will go back
  1197. ;                     to the Device Descriptor question.
  1198. ; Syntax:           _viInteractiveControl($s_command_save_filename = "")
  1199. ; Parameter(s):     $s_command_save_filename - This is an OPTIONAL PARAMETER
  1200. ;                     The name of the file in which the SCPI commands issued
  1201. ;                     during the interactive session will be saved.
  1202. ;                     If no filename is passed the funcion asks the user if and
  1203. ;                     where does the user want to save the issued commands.
  1204. ; Requirement(s):   The VISA libraries must be installed (you can check whether
  1205. ;                   visa32.dll is in {WINDOWS}\system32)
  1206. ;                   For GPIB communication a GPIB card (such as a National Instruments
  1207. ;                   NI PCI-GPIB card or an Agilent 82350B PCI High-Performance GPIB card
  1208. ; Return Value(s):  The list of AutoIt3 VISA commands that were executed by the tool.
  1209. ;                   This is the same list that is saved into the file if the a
  1210. ;                   filename is passed to the function.
  1211. ; Author(s):        Angel Ezquerra <ezquerra at gmail dot com>
  1212. ; Note(s):          Type "FIND" in the Device Descriptor query to perform a GPIB
  1213. ;                   search
  1214. ;
  1215. ;===============================================================================
  1216. Func _viInteractiveControl($s_command_save_filename = "")
  1217.     ;- Define variables, set their default values
  1218.     Local $s_vi_id = "FIND" ; "GPIB::1::0" ; Default values
  1219.     Local $s_command = "*IDN?"
  1220.     Local $i_timeout_ms = 10000 ; ms
  1221.     Local $s_answer = ""
  1222.     Local $a_descriptor_list[1], $a_idn_list[1] ; The results of the GPIB search
  1223.     ; The variables used to save the commands to a file
  1224.     Local $s_empty_command_list = "#include <Visa.au3>" & @CR & @CR & "Local $s_answer" & @CR & @CR
  1225.     Local $s_new_command = ""
  1226.     Local $s_command_list = $s_empty_command_list
  1227.  
  1228.     ;- Loop until the user Cancles the Instrument Device Descriptor request
  1229.     While 1
  1230.         ;- Request the Instrument Descriptor (reuse the previous descriptor)
  1231.         $s_vi_id = InputBox("Instrument Device Descriptor", _
  1232.                 "- Type the Instrument Device Descriptor (e.g. 'GPIB::1::0' or 'GPIB::1::INSTR')" & _
  1233.                 @CR & @CR & _
  1234.                 "- Type FIND to perform a GPIB search" & _
  1235.                 @CR & @CR & _
  1236.                 "- Click CANCEL to STOP the VISA interactive tool", $s_vi_id, "", 500, 250)
  1237.         If @error == 1 Then
  1238.             ; The Cancel button was pushed -> Exit the loop
  1239.             ExitLoop
  1240.         EndIf
  1241.         If StringUpper($s_vi_id) == "FIND" Then
  1242.             ; Perform a GPIB search
  1243.             $s_command_list = $s_command_list & _
  1244.                     "Local $a_descriptor_list[1], $a_idn_list[1]" & @CR & @CR & _
  1245.                     "_viFindGpib($a_descriptor_list, $a_idn_list, 1)" & @CR & @CR
  1246.             _viFindGpib($a_descriptor_list, $a_idn_list, 1)
  1247.             If UBound($a_descriptor_list) >= 1 Then
  1248.                 ; If an instrument was found, use the 1st found instrument as the default
  1249.                 ; for the next query
  1250.                 $s_vi_id = $a_descriptor_list[0]
  1251.             EndIf
  1252.             ContinueLoop
  1253.         EndIf
  1254.  
  1255.         ;- Request the command that must be executed (reuse the previous command)
  1256.         $s_answer = InputBox("SCPI command", "Type the SCPI command", $s_command)
  1257.         If @error == 1 Then
  1258.             ; The Cancel button was pushed -> Restart the process
  1259.             ContinueLoop
  1260.         EndIf
  1261.         $s_command = $s_answer ; We got a valid command
  1262.  
  1263.         ;- Request the timeout (reuse the previous timout)
  1264.         $s_answer = InputBox("Command Timeout (ms)", _
  1265.                 "Type the command timeout (in milliseconds)", $i_timeout_ms)
  1266.         If @error == 1 Then
  1267.             ; The Cancel button was pushed -> Restart the process
  1268.             ContinueLoop
  1269.         EndIf
  1270.         $i_timeout_ms = 0 + $s_answer ; We got a valid timeout
  1271.  
  1272.         ;- Add the command to the command list
  1273.         $s_new_command = '$s_answer = _viExecCommand("' & $s_vi_id & '", "' & _
  1274.                 $s_command & '", ' & $i_timeout_ms & ')'
  1275.         $s_command_list = $s_command_list & $s_new_command & @CR
  1276.  
  1277.         ;- Execute the requested command
  1278.         $s_answer = _viExecCommand($s_vi_id, $s_command, $i_timeout_ms)
  1279.  
  1280.         If IsString($s_answer) Then
  1281.             ;- The command was a query and the instrument answered it
  1282.             ; Show the query results
  1283.             MsgBox(64, "Query results", "[" & $s_vi_id & "] " & $s_command & " -> " & $s_answer)
  1284.         ElseIf $s_answer == 0 Then
  1285.             ;- The command was not a query but it was exuced successfully
  1286.             MsgBox(64, "Command result", "The command:" & @CR & @CR & _
  1287.                     "         '" & $s_command & "'" & @CR & @CR & _
  1288.                     "was SUCCESSFULLY executed on the device: " & @CR & @CR & _
  1289.                     "         '" & $s_vi_id & "'")
  1290.         ElseIf $s_answer < 0 Then
  1291.             ;- There was an error -> Show an error message
  1292.             $s_answer = MsgBox(16 + 4, "VISA Error", _
  1293.                     "There was a VISA error when executing the command:" & @CR & @CR & _
  1294.                     "'" & $s_command & "'" & @CR & @CR & "on the Device '" & $s_vi_id & "'" & _
  1295.                     @CR & @CR & _
  1296.                     "Do you want to RESET the GPIB bus before continuing?")
  1297.             If $s_answer == 6 Then ; Yes
  1298.                 _viGpibBusReset()
  1299.                 MsgBox(0, "VISA", "The GPIB bus was RESET!")
  1300.             EndIf
  1301.         EndIf
  1302.     WEnd
  1303.  
  1304.     If $s_command_list <> $s_empty_command_list Then
  1305.         ; If at least one command was issued we might want to save the file
  1306.  
  1307.         If $s_command_save_filename == "" Then
  1308.             ; The user did not pass an explicit file name in which to save the commands
  1309.             ; Ask him if he wants to save the m now
  1310.             $s_answer = MsgBox(64 + 4, "Save commands to AutoIt3 script?", _
  1311.                     "Do you want to save the commands that you issued into an AutoIt3 script?")
  1312.             If $s_answer == 6 Then ; Yes
  1313.                 $s_command_save_filename = FileSaveDialog("Save as...", @ScriptDir, _
  1314.                         "AutoIt3 scripts (*.au3)", 16, "visa_log.au3")
  1315.                 If @error <> 0 Then
  1316.                     $s_command_save_filename = ""
  1317.                 EndIf
  1318.             EndIf
  1319.         EndIf
  1320.  
  1321.         If $s_command_save_filename <> "" Then
  1322.             ;- Save the SCPI commands into a file
  1323.             If FileExists($s_command_save_filename) Then
  1324.                 ; Delete the save file if it already exists
  1325.                 FileDelete($s_command_save_filename)
  1326.             EndIf
  1327.             FileWrite($s_command_save_filename, $s_command_list)
  1328.         EndIf
  1329.     EndIf
  1330.  
  1331.     Return $s_command_list ; Return the list of executed commands
  1332. EndFunc   ;==>_viInteractiveControl